""" 
Copyright 2009 Texas A&M University
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0
  
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License.
"""

from logging import getLogger

from zope.interface import implements
from zope.component import queryUtility

from Products.Archetypes import atapi
from Products.ATContentTypes.content import base, schemata

from meercat.ui.plone import config
from meercat.ui.plone.interfaces import IMeercatConnectionManager
from meercat.ui.plone.content.interfaces import IMeercatSearch, sortFields, queryHandlers



log = getLogger('meercat.ui.plone.content.search')

class MeercatSearch(base.ATCTContent):
    """A Library Subject Guide
    """
    implements(IMeercatSearch)
    #TODO: Pretty labels/descriptions for all fields with i18n
    schema = schemata.ATContentTypeSchema.copy() + atapi.Schema((
        atapi.LinesField('filterQueries',
                         storage=atapi.AnnotationStorage(),
                         schemata='default'),
        atapi.LinesField('queryFields',
                         storage=atapi.AnnotationStorage(),
                         schemata='default'),
        atapi.StringField('defaultSortField',
                          schemata='default',
                          vocabulary=sortFields,
                          enforceVocabulary=True,
                          storage=atapi.AnnotationStorage(),
                          widget=atapi.SelectionWidget()),
        atapi.BooleanField('allowSortSelection',
                           storage=atapi.AnnotationStorage(),
                           schemata='default'),
        atapi.StringField('queryHandler',
                          schemata='default',
                          vocabulary=queryHandlers,
                          enforceVocabulary=True,
                          storage=atapi.AnnotationStorage(),
                          widget=atapi.SelectionWidget()),))
    
    filterQueries = atapi.ATFieldProperty('filterQueries')
    queryFields = atapi.ATFieldProperty('queryFields')
    defaultSortField = atapi.ATFieldProperty('defaultSortField')
    allowSortSelection = atapi.ATFieldProperty('allowSortSelection')
    queryHandler = atapi.ATFieldProperty('queryHandler')
    
    def getMeercatManager(self):
        return queryUtility(IMeercatConnectionManager)
    
    def getQueryParameters(self):
        """ Return a dictionary of the query parameters """
        params = {}
        
        if self.queryFields and len(self.queryFields) > 0:
            params['qf'] = ' '.join(self.queryFields)
        
        if self.queryHandler:
            params['qt'] = self.queryHandler
        
        if self.defaultSortField:
            params['sort'] = self.defaultSortField
        
        if self.filterQueries and len(self.filterQueries) > 0:
            params['fq'] = list(self.filterQueries)
        
        return params

atapi.registerType(MeercatSearch, config.PROJECTNAME)
